package org.kjtc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.kjtc.entity.EquipmentPowerType;
import org.kjtc.entity.Page;
import org.kjtc.service.ActionHistoryService;
import org.kjtc.service.EquipmentPowerTypeService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class EquipmentPowerTypeController extends BaseController{

	@Resource(name="equipmentPowerTypeService")
	private EquipmentPowerTypeService  equipmentPowerTypeService;

	@Resource(name = "actionHistoryService")
	private ActionHistoryService actionHistoryService;

  /**
   * 设备类型查询
   * @param equipmentType
   * @param page
   * @param rows
   * @return
   */
  @RequestMapping(value="/searchEquipmentPowerType")
  @ResponseBody
  public Map<String, Object> searchEquipmentPowerType(EquipmentPowerType equipmentPowerType, String page, String rows){

	  Map<String, Object> map = new HashMap<String, Object>();
	  	logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备类型查询开始");
		// 分页
		Page pageQuery = getPage();
		pageQuery.setCurrentPage(Integer.parseInt(page));
		pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
		pageQuery.setShowCount(Integer.parseInt(rows));

		List<EquipmentPowerType> list = null;
		try {
			list = equipmentPowerTypeService.getEquipmentPowerTypePage(equipmentPowerType, pageQuery);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备类型查询异常" + e.getMessage());
			return map;
		}
		map.put("totalCnt",  pageQuery.getTotalResult());
		map.put("totalPage", pageQuery.getTotalPage());
		map.put("equipmentTypeList",list);
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备类型查询结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "设备类型查询");
		equipmentPowerType = null;
	    return map;
  }
  /**
   * 设备类型添加
   * @param equipmentType
   * @return
   */
  @RequestMapping(value="/saveEquipmentPowerType")
  @ResponseBody
  public Map<String, Object> saveEquipmentPowerType(EquipmentPowerType equipmentPowerType) {

	  Map<String, Object> map = new HashMap<String, Object>();
	  	logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备类型添加开始");
		int count = 0;
		try {
				equipmentPowerType.setCreateUser(this.getUesr().getUserID());
				equipmentPowerType.setUpdateUser(this.getUesr().getUserID());
				count = equipmentPowerTypeService.insertEquipmentPowerType(equipmentPowerType);
				if(count > 0){
					map.put("result", "Success");
				}else{
					map.put("result", "NotFound");
				}
			} catch (DuplicateKeyException ex) {
				String[] error = ex.getCause().getMessage().split("'");
				if ("PRIMARY".equals(error[3].trim())) {
					map.put("result", "EquipmentPowerTypeNoNull");
				} else if ("EquipmentPowertypeName".equals(error[3].trim())){
					map.put("result", "EquipmentPowertypeNameNoNoNull");
				}
			} catch (Exception e) {
			    e.printStackTrace();
			    map.put("result", "Error");
			    logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备类型添加异常" + e.getMessage());
		    }
			logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备类型添加结束");
			actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "设备类型添加");
			equipmentPowerType = null;
			return map;
		}

  /**
   * 设备类型修改
   * @param equipmentType
   * @return
   */

	@RequestMapping(value="/updateEquipmentPowerType")
	@ResponseBody
	public Map<String,Object> updateEquipmentPowerType(EquipmentPowerType equipmentPowerType){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备类型修改开始");
		int count = 0;
		try {
			equipmentPowerType.setUpdateUser(this.getUesr().getUserID());
			count = equipmentPowerTypeService.updateEquipmentPowerType(equipmentPowerType);
			if(count > 0){
				map.put("result", "Success");
			}else {
				map.put("result", "NotFound");
			}
		} catch (DuplicateKeyException ex) {
			String[] error = ex.getCause().getMessage().split("'");
			if ("EquipmentPowerType".equals(error[3].trim())) {
				map.put("result", "EquipmentPowerTypeNoNull");
			} else if ("EquipmentPowertypeName".equals(error[3].trim())){
				map.put("result", "EquipmentPowertypeNameNoNoNull");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
		    logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备类型修改异常" + e.getMessage());
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备类型修改结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "设备类型修改");
		equipmentPowerType = null;
		return map;
	}

	@RequestMapping(value = "/deleteEquipmentPowerType")
	@ResponseBody
	public Map<String, Object> deleteEquipmentPowerType(String equipmentPowerTypeList){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备类型删除开始");
		EquipmentPowerType equipmentPowerType = new EquipmentPowerType();
		equipmentPowerType.setEquipmentPowerTypeList(equipmentPowerTypeList.split(","));
		int count = 0;
		try {
			count = equipmentPowerTypeService.deleteEquipmentPowerType(equipmentPowerType);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
		    logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备类型删除异常" + e.getMessage());
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备类型删除结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "设备类型删除");
		equipmentPowerType = null;
		return map;
	}
}
